﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace GUI
{
    public partial class KhuyenMai : Form
    {
        public KhuyenMai()
        {
            InitializeComponent();
        }

        private bool boolNgayBDTu = false;
        private bool boolNgayBDDen = false;
        private bool boolNgayKTTu = false;
        private bool boolNgayKTDen = false;

        private void KhuyenMai_Activated(object sender, EventArgs e)
        {
            KhoiTao();
        }

        private void KhoiTao()
        {
            List<KhuyenMaiDTO> lsKhuyenMaiDTO = KhuyenMaiBUS.QueryAll();

            if (lsKhuyenMaiDTO.Count >= 0)
            {
                KhuyenMaiDTO item = new KhuyenMaiDTO();
                dataGridView_khuyenMai.Rows.Clear();
                dataGridView_khuyenMai.ColumnCount = 5;
                for (int i = 0; i < lsKhuyenMaiDTO.Count; i++)
                {
                    item = lsKhuyenMaiDTO[i];
                    string tenMon = MonAnBUS.SelectMonAnById(item.Ma_MonAn).TenMon;

                    dataGridView_khuyenMai.Rows.Add(item.Ma_KhuyenMai, tenMon, item.PhanTramGiam.ToString(), item.NgayBatDau.ToString(), item.NgayKetThuc.ToString());
                    //dataGridView_khuyenMai.Rows.Add((i + 1).ToString(),item.Ma_KhuyenMai);
                    //MessageBox.Show(item.Ma_KhuyenMai);
                    dataGridView_khuyenMai.ReadOnly = true;
                }
            }
            else
            {
                MessageBox.Show("entry");
            }

        }

        private void Search()
        {
            DateTime dateNgayBDTu = new DateTime();
            DateTime dateNgayBDDen = new DateTime();
            DateTime dateNgayBDHienTai = new DateTime();

            DateTime dateNgayKTTu = new DateTime();
            DateTime dateNgayKTDen = new DateTime();
            DateTime dateNgayKTHienTai = new DateTime();

            if (boolNgayBDTu)
            {
                dateNgayBDTu = dateTimePicker_ngayBatDau_Tu.Value.Date;
            }
            else
            {
                dateNgayBDTu = new DateTime(1800, 1, 1).Date;
            }
            if (boolNgayBDDen)
            {
                dateNgayBDDen = dateTimePicker_ngayBatDau_Den.Value.Date;
            }
            else
            {
                dateNgayBDDen = DateTime.Now.Date;
            }

            if (boolNgayKTTu)
            {
                dateNgayKTTu = dateTimePicker_ngayKetThuc_Tu.Value.Date;
            }
            else
            {
                dateNgayKTTu = new DateTime(1800, 1, 1).Date;
            }

            if (boolNgayKTDen)
            {
                dateNgayKTDen = dateTimePicker_ngayKetThuc_Den.Value.Date;
            }
            else
            {
                dateNgayKTDen = DateTime.Now.Date;
            }

            int vt = -1;
            //for (int i = dataGridView_khuyenMai.RowCount - 1; i >= 0; i--)
            //{
            //    dataGridView_khuyenMai.Rows[i].Visible = false;

            //    if (dataGridView_khuyenMai.Rows[i].Cells["clTenMon"].Value.ToString().ToUpper().IndexOf(txtTenMon.Text.ToString().ToUpper()) >= 0
            //        )
            //    {
            //        vt = i;
            //        dataGridView_khuyenMai.Rows[i].Visible = true;
            //    }
            //}
            for (int i = 0; i < dataGridView_khuyenMai.RowCount; i++)
            {
                string[] temp = dataGridView_khuyenMai.Rows[i].Cells[clNgayBD.Index].Value.ToString().Trim().Split(' ');
                string[] chuoiNgayBD = temp[0].Split('/');
                dateNgayBDHienTai = new DateTime(int.Parse(chuoiNgayBD[2]), int.Parse(chuoiNgayBD[0]), int.Parse(chuoiNgayBD[1])).Date;

                temp = dataGridView_khuyenMai.Rows[i].Cells[clNgayKT.Index].Value.ToString().Trim().Split(' ');
                string[] chuoiNgayKT = temp[0].Split('/');
                dateNgayKTHienTai = new DateTime(int.Parse(chuoiNgayKT[2]), int.Parse(chuoiNgayKT[0]), int.Parse(chuoiNgayKT[1])).Date;

                dataGridView_khuyenMai.Rows[i].Visible = false;

                if (dataGridView_khuyenMai.Rows[i].Cells["clTenMon"].Value.ToString().ToUpper().IndexOf(txtTenMon.Text.ToString().ToUpper()) >= 0
                    && dateNgayBDTu.CompareTo(dateNgayBDHienTai) <= 0 && dateNgayBDDen.CompareTo(dateNgayBDHienTai) >= 0
                    && dateNgayBDTu.CompareTo(dateNgayBDDen) <= 0
                    && dateNgayKTTu.CompareTo(dateNgayKTHienTai) <= 0 && dateNgayKTDen.CompareTo(dateNgayKTHienTai) >= 0
                    && dateNgayKTTu.CompareTo(dateNgayKTDen) <= 0
                    )
                {
                    dataGridView_khuyenMai.Rows[i].Visible = true;
                    vt = i;
                    break;
                }

            }

            for (int i = vt+1; i < dataGridView_khuyenMai.RowCount; i++)
            {
                string[] temp = dataGridView_khuyenMai.Rows[i].Cells[clNgayBD.Index].Value.ToString().Trim().Split(' ');
                string[] chuoiNgayBD = temp[0].Split('/');
                dateNgayBDHienTai = new DateTime(int.Parse(chuoiNgayBD[2]), int.Parse(chuoiNgayBD[0]), int.Parse(chuoiNgayBD[1])).Date;

                temp = dataGridView_khuyenMai.Rows[i].Cells[clNgayKT.Index].Value.ToString().Trim().Split(' ');
                string[] chuoiNgayKT = temp[0].Split('/');
                dateNgayKTHienTai = new DateTime(int.Parse(chuoiNgayKT[2]), int.Parse(chuoiNgayKT[0]), int.Parse(chuoiNgayKT[1])).Date;

                dataGridView_khuyenMai.Rows[i].Visible = false;

                if (dataGridView_khuyenMai.Rows[i].Cells["clTenMon"].Value.ToString().ToUpper().IndexOf(txtTenMon.Text.ToString().ToUpper()) >= 0
                    && dateNgayBDTu.CompareTo(dateNgayBDHienTai) <= 0 && dateNgayBDDen.CompareTo(dateNgayBDHienTai) >= 0
                    && dateNgayBDTu.CompareTo(dateNgayBDDen) <= 0
                    && dateNgayKTTu.CompareTo(dateNgayKTHienTai) <= 0 && dateNgayKTDen.CompareTo(dateNgayKTHienTai) >= 0
                    && dateNgayKTTu.CompareTo(dateNgayKTDen) <= 0
                    )
                {
                    dataGridView_khuyenMai.Rows[i].Visible = true;                    
                }

            }

            if (vt != -1)
                {
                    dataGridView_khuyenMai.CurrentCell = dataGridView_khuyenMai.Rows[vt].Cells[0];
                    dataGridView_khuyenMai.CurrentCell.Selected = true;
                }

        }

        private void txtTenMon_TextChanged(object sender, EventArgs e)
        {
            Search();
        }

        private void dateTimePicker_ngayBatDau_Tu_ValueChanged(object sender, EventArgs e)
        {
            boolNgayBDTu = true;
            Search();
        }

        private void dateTimePicker_ngayBatDau_Den_ValueChanged(object sender, EventArgs e)
        {
            boolNgayBDDen = true;
            Search();
        }

        private void dateTimePicker_ngayKetThuc_Tu_ValueChanged(object sender, EventArgs e)
        {
            boolNgayKTTu = true;
            Search();
        }

        private void dateTimePicker_ngayKetThuc_Den_ValueChanged(object sender, EventArgs e)
        {
            boolNgayKTDen = true;
            Search();
        }
    }
}
